<template>
  <!--  通知通告维护  -->
  <div>
    <!-- 搜索 -->
    <TopFilter
      :list="topFilterInit"
      :cols="3"
      labelWidth="90"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
    ></TopFilter>
    <!-- 按钮 -->

    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :isSelectColumn="false"
      :onRowSelectChange="selectChange"
      columnsRef="notification_maintain_index_01"
      :onColumnsChange="columns => this.columns = columns"
      height="auto"
    >
      <template slot="controls">
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="detailHandle(null,'add')"
        >新增</el-button>
        <!-- <el-button
          size="small"
          type="primary"
          icon="el-icon-remove-outline"
          @click="withdraw"
          :loading="saveLoading"
        >撤回</el-button>-->
      </template>
    </FilterTable>

    <!-- 新增窗口 -->
    <Drawer
      :visible.sync="visibleDrawer"
      :title="title"
      :width="920"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <info-modal v-model="initData" @close="infoClose" />
    </Drawer>
  </div>
</template>

<script>
import infoModal from './info';
import { pageNotificationMaintaion, withdrawNotificationByIds, deleteNotificationById } from '@/api/cms-api/notification/maintain/index';
import { dictionary } from '@/utils/dictMixin';
import { parseTime } from '@/utils/index';
// import { getLoginUser } from '@/api/cms-api/projectOperating/program/departmentInquire';
import { findFinanceCompany } from '@/api/cms-api/origin/originInfo/OEM';
export default {
  name:'notificationMaintain',
  mixins: [dictionary],
  components: {
    infoModal
  },
  data() {
    return {
      topFilterInit: this.topFilterInitFun(),
      columns: this.creatHeader(),
      fetchApi: pageNotificationMaintaion, //api 接口 table请求数据入口
      fetchParams: null,
      visibleDrawer: false,
      initData: {},
      title: '通知通告新增',
      saveLoading: false,
      rowSelectData: []
    };
  },
  mounted() {
    // this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods: {
    topFilterInitFun() {
      return [
        { type: 'SELECT', label: '类别', fieldName: 'notificationType', placeholder: '请选择', initialValue: '', itemList: this.createDictList('1561') },
        { type: 'INPUT', label: '主题', fieldName: 'notificationTitle', placeholder: '请输入', initialValue: '' },
        { type: 'RANGE_DATE', label: '发布时间', fieldName: 'publisDate', placeholder: '请选择', initialValue: '' },
        { type: 'INPUT', label: '发布人', fieldName: 'publishPersonName', placeholder: '请输入', initialValue: '' },
        { type: 'SELECT', label: '状态', fieldName: 'status', placeholder: '请选择', initialValue: '', itemList: this.createDictList('1564') }
      ];
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 200,
          fixed: 'left',
          render: props => {
            let disabled = props.row.status == '15641002' || props.row.status == '15641004' ? true : false;
            let cannotwithDraw = props.row.status != '15641002';
            return (
              <div>
                <el-button size="mini" type="text" disabled={disabled} onClick={() => this.editHandle(props.row, 'edit')}>
                  编辑
                </el-button>
                <el-divider direction="vertical" />
                <el-button size="mini" type="text" onClick={() => this.editHandle(props.row, 'show')}>
                  详情
                </el-button>
                <el-divider direction="vertical" />
                <el-button size="mini" type="text" disabled={disabled} onClick={() => this.deleteItem(props.row)}>
                  删除
                </el-button>
                <el-divider direction="vertical" />
                <el-button size="mini" type="text" disabled={cannotwithDraw} onClick={() => this.withdraw(props.row)}>
                  撤回
                </el-button>
              </div>
            );
          }
        },
        {
          title: '类别',
          dataIndex: 'notificationType',
          sorter: true,
          filterItems: this.createDictList('1561'),
          render: props => {
            return <span>{this.createDictText(props.row.notificationType, '1561')}</span>;
          }
        },
        { title: '主题', dataIndex: 'notificationTitle', sorter: true },
        { title: '编号', dataIndex: 'notificationNo', sorter: true },
        {
          title: '紧急程度',
          dataIndex: 'urgency',
          sorter: true,
          filterItems: this.createDictList('1562'),
          render: props => {
            return <span>{this.createDictText(props.row.urgency, '1562')}</span>;
          }
        },
        { title: '发起部门', dataIndex: 'publishOrgName', sorter: true },
        { title: '发布人', dataIndex: 'publishPersonName', sorter: true },
        {
          title: '状态',
          dataIndex: 'status',
          sorter: true,
          filterItems: this.createDictList('1564'),
          render: props => {
            return <span>{this.createDictText(props.row.status, '1564')}</span>;
          }
        },
        {
          title: '是否全网发布',
          dataIndex: 'isAll',
          sorter: true,
          filterItems: this.createDictList('1004'),
          render: props => {
            return <span>{this.createDictText(props.row.isAll, '1004')}</span>;
          }
        },
        { title: '有效期至', dataIndex: 'endTime', sorter: true, dateFormat: 'yyyy-MM-dd' },
        { title: '发布时间', dataIndex: 'publishDate', sorter: true, dateFormat: 'yyyy-MM-dd HH:mm' }
      ];
    },
    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
      this.fetchParams.publisStartDate = this.fetchParams.publisDate[0];
      this.fetchParams.publisEndDate = this.fetchParams.publisDate[1];
      console.log('搜索框数据：', this.fetchParams);
      console.log('ccc', this.$route);
    },
    //展开收起时触发
    collapseHandle() {},
    // 新增  编辑 详情
    detailHandle(row, type) {
      this.title = '通知通告新增';
      this.initData = {
        type: type, // add-edit-show
        id: 1001
      }; //行id
      this.visibleDrawer = true;
    },
    editHandle(row, type) {
      if (type === 'edit') {
        this.title = '通知通告编辑';
      } else if (type === 'show') {
        this.title = '通知通告详情';
      }
      this.initData = {
        type: type, // add-edit-show
        notificationId: row.notificationId,
        id: 1001
      }; //行id
      this.visibleDrawer = true;
    },
    //关闭详情 rData 是页面关闭回调回来的数据
    infoClose(rData) {
      this.visibleDrawer = false;
      if (rData.type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    // 撤回
    async withdraw(val) {
      this.rowSelectData = [];
      this.rowSelectData.push(val);
      console.log('row', this.rowSelectData);
      //  this.rowSelectData = val;
      if (this.rowSelectData === undefined || this.rowSelectData.length === 0) {
        this.$message({
          type: 'error',
          message: '请选择您要撤回的通知通告！'
        });
        return;
      }

      this.$confirm('是否撤回', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          var ids = this.packageNotificationId(this.rowSelectData);
          if (ids === -1) {
            this.$message({
              type: 'error',
              message: '请选择已发布的通知通告进行撤回！'
            });
            return;
          }
          this.saveLoading = true;
          var res = await withdrawNotificationByIds(ids);
          this.validReturnDate(res);
        })
        .catch(() => {
          this.$notify.info({ title: '提示信息', message: '操作取消' });
        });
    },
    validReturnDate(res) {
      if (res.resultCode === 200) {
        this.saveLoading = false;
        this.$notify.success({ title: '提示信息', message: '操作成功!' });
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      } else {
        this.saveLoading = false;
        this.$notify.error({ title: '提示信息', message: res.errMsg || '操作失败!' });
      }
    },
    selectChange(val) {
      console.log('val', val);
      this.rowSelectData = val;
    },
    packageNotificationId(data) {
      var ids = [];
      var flag = true;
      for (var i = 0; i < data.length; i++) {
        console.log('状态：', data[i].status);
        if (data[i].status != 15641002) {
          flag = false;
        }
        ids.push(data[i].notificationId);
      }
      if (flag) {
        return ids;
      } else {
        return -1;
      }
    },
    // 删除
    async deleteItem(data) {
      this.$confirm('是否删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          console.log('data:', data.notificationId);
          var res = await deleteNotificationById(data.notificationId);
          this.validReturnDate(res);
        })
        .catch(() => {
          this.$notify.info({ title: '提示信息', message: '操作取消' });
        });
    }
  }
};
</script>

<style lang="less" scoped>
</style>
